var express = require('express');
var router = express.Router();
const timeStamp = require('time-stamp');

const mysql = require('mysql');
let connObj = mysql.createConnection({
    host: 'localhost',
    port: '3306',
    user: 'root',
    password: 'root',
    database: 'stu1',
})

function Query(sql) {
    return new Promise((resolve, reject) => {
        connObj.query(sql, (err, result) => {
            resolve({
                err,
                result
            });
        })
    })
}

/* GET home page. */
router.get('/', function (req, res, next) {
    res.render('index', {
        title: 'Express'
    });
});

router.get('/login', (req, res) => {
    res.render('login', {
        tips: ''
    })
})

router.get('/register', (req, res) => {
    res.render('register')
})

router.get('/users', (req, res) => {
    res.render('users')
})

router.post('/login', async (req, res) => {
    let {
        username,
        password
    } = req.body;
    if (username == '' || password == '') {
        // res.render(','输入不能为空');
        res.render('login', {
            tips: '输入不能为空'
        });
        return;
    }
    let sql = `select * from users where '${username}' = users.username and '${password}' = users.password`

    //async方法执行 返回一个对象 包括err和result

    let d = await Query(sql);
    if (d.err) {
        res.render('login', {
            tips: '请稍后再试'
        });
        return;
    }
    if (d.result.length >= 1) {
        let users = [{
            username,
            password
        }];
        res.cookie('username', username)
        res.cookie('password', password)
        res.render('users', {
            users,
            title: '登录',
        });
        // res.send('登录成功')
        return;
    }
    // res.send('用户名或密码输入错误');
    res.render('login', {
        tips: '用户名或密码输入错误'
    })

})

//注册业务
router.post('/register', async (req, res) => {
    let {
        username,
        password,
        password1
    } = req.body;
    if (username == '' || password == '' || password1 == '') {
        res.send('不能为空');
        return;
    }
    if (password !== password1) {
        res.send('俩次密码输入不一致 请重新输入');
        return;
    }
    let sql = `select * from users where '${username}' = users.username`

    let b = await Query(sql);
    if (b.err) {
        res.send('请稍后再试');
        return;
    }
    if (b.result.length == 0) {
        if (username && password && password1) {
            let users = [{
                username,
                password
            }]
            let time = timeStamp('YYYY-MM-DD HH:mm:ss');
            let insert = `insert into users (username,password,createtime) values ('${username}','${password}','${time}')`
            let aaa = await Query(insert);
            console.log(aaa);
            // res.render('users', {
            //     users,
            //     title: '注册',
            // })
            res.send('✔')
            return;
        } else {
            res.send('✔')
            return;
        }
    }
    res.send('账户已被注册');
})

module.exports = router;